package com.tencent.externalstoragebar;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.tencent.externalstoragebar.publicthing.PublicData;
import com.tencent.externalstoragebar.service.FloatingWindowService;
import com.tencent.externalstoragebar.service.StorageService;
import com.tencent.externalstoragebar.service.GetSlotService;
import com.tencent.externalstoragebar.tools.FloatTool;
import com.tencent.externalstoragebar.tools.l;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_x;      //x坐标
    private EditText et_y;      //y坐标
    private EditText et_r;      //r半径
    private EditText et_r_in;   //内半径
    private EditText et_color;  //颜色
    private SeekBar sb_bar;     //透明度
    private EditText et_a;      //判断区域x坐标

    private EditText et_b;      //判断区域y坐标

    private EditText et_c;      //判断区域半径
    private EditText et_time;      //时间
    private Button btn_create;  //创建按钮

    //是否已经创建
    public static boolean isCreate=false;

    //测试按钮---------------
    private Button btn_test;
    private Button btn_test2;
    //----------------------

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化组件方法
        initView();

        //加载数据
        loadDataFromSP();

        //按钮点击事件监听器
        btn_create=findViewById(R.id.btn_create);
        btn_create.setOnClickListener(this);

        //测试按钮--------------------------
        btn_test=findViewById(R.id.btn_test);
        btn_test.setOnClickListener(this);
        btn_test2=findViewById(R.id.btn_test2);
        btn_test2.setOnClickListener(this);
        //---------------------------------
    }

    //初始化组件方法
    private void initView(){

        //初始化组件
        et_x = findViewById(R.id.et_x);
        et_y = findViewById(R.id.et_y);
        et_r = findViewById(R.id.et_r);
        et_r_in = findViewById(R.id.et_r_in);
        et_color = findViewById(R.id.et_color);
        sb_bar = findViewById(R.id.sb_bar);
        et_a=findViewById(R.id.et_a);
        et_b=findViewById(R.id.et_b);
        et_c=findViewById(R.id.et_c);
        et_time=findViewById(R.id.et_time);

    }

    //点击事件
    @Override
    public void onClick(View view) {
        int id = view.getId();

        //启动按钮
        l.d("被点击了");
        if (id==R.id.btn_create){
            //如果已经创建
            if (isCreate){
                //消耗悬浮窗
                FloatingWindowService.windowManager.removeView(FloatingWindowService.backgroundView);
                FloatingWindowService.windowManager.removeView(FloatingWindowService.floatingView);

                isCreate=false;
            }
            //判断输入框是否有空
            if (haveEmpty()){
                Toast.makeText(MainActivity.this, "输入框有未输入的", Toast.LENGTH_SHORT).show();
                return;
            }

            //设置权限
            FloatTool.RequestOverlayPermission(this);

            //保存当前数据到公共数据中
            saveToPublicData();

            //将公共数据的内容保存到SharedPreferences
            saveDataToSP();

            //启动获取槽位服务
            l.d("启动获取槽位服务");
            Intent intentGet=new Intent(MainActivity.this, GetSlotService.class);
            startService(intentGet);

            //开启一条线程,当获取到槽位后,开启核心服务
            new Thread(new Runnable() {
                @Override
                public void run() {
                    while (!GetSlotService.over){
                        //只要没获取到槽位就一直卡在这里
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        l.d("等待中...");
                    }
                    //到这里说明获取到了槽位
                    //启动核心服务
                    l.d("启动核心服务");
                    actionService();
                }
            }).start();

            //标记悬浮窗不可创建
            isCreate=true;

            //设置按钮不可以点击
            btn_create.setEnabled(false);

            //设置按钮文本为已经创建
            btn_create.setText("已经创建");

            //设置文本
            ((TextView)findViewById(R.id.tv_talk))
                    .setText("ok,请触摸一下屏幕槽位(摸一下就行)" +
                            "\n"+"看见荧光悬浮窗即为成功"+
                            "\n"+"(不要按任何按钮,除非你已经摸了)");
            /*
            "2024/5/1 19:15\n"+
                            "蓄力条功能基本完善\n"+
                            "差颜色选择功能\n"+
                            "差悬浮窗移动功能\n"+
                            "早松手提示等驱动造玩在搞\n"+
                            "推荐x=1157   "+
                            "y=540   "+
                            "半径=40   空心=15\n"+
                            "a=270   "+
                            "b=1938   "+
                            "c=90\n"+
                            "加油吧!\n"*/
        }
        else if (id==R.id.btn_test){
            //测试按钮
            l.d("测试按钮");
            test();
        }else if (id==R.id.btn_test2){
            //测试按钮
            l.d("测试按钮");
            test2();
        }
    }

    //测试方法(将所有输入框赋予初始值)
    private void test(){

        l.d("测试方法");
        et_x.setText("1157");
        et_y.setText("540");
        et_r.setText("40");
        et_r_in.setText("15");
        et_color.setText("16777215");
        sb_bar.setProgress(125);
        et_a.setText("270");
        et_b.setText("1938");
        et_c.setText("90");
        et_time.setText("1000");
    }

    //测试方法2(将所有输入框赋予初始值)
    private void test2(){

        l.d("测试方法2");
        et_x.setText("1140");
        et_y.setText("540");
        et_r.setText("40");
        et_r_in.setText("15");
        et_color.setText("114514");
        sb_bar.setProgress(125);
        et_a.setText("270");
        et_b.setText("1945");
        et_c.setText("90");
        et_time.setText("1000");
    }

    //启动核心服务方法
    public void actionService(){
        //启动悬浮窗服务
        Intent intent=new Intent(MainActivity.this, FloatingWindowService.class);
        startService(intent);

        //启动触摸蓄力服务
        Intent intent1=new Intent(MainActivity.this, StorageService.class);
        startService(intent1);
    }

    //将公共数据的内容保存到SharedPreferences方法
    private void saveDataToSP(){

        SharedPreferences.Editor editor=getSharedPreferences("data",MODE_PRIVATE).edit();

        editor.putInt("x", PublicData.x);
        editor.putInt("y", PublicData.y);
        editor.putInt("r", PublicData.r);
        editor.putInt("rIn", PublicData.rIn);
        editor.putInt("color", PublicData.color);
        editor.putInt("alpha", PublicData.alpha);

        editor.putInt("a", PublicData.a);
        editor.putInt("b", PublicData.b);
        editor.putInt("c", PublicData.c);

        editor.putInt("time", PublicData.time);

        editor.apply();
    }

    //保存当前数据到公共数据中方法
    private void saveToPublicData(){

        //获取输入框的值
        int x = Integer.parseInt(et_x.getText().toString());
        int y = Integer.parseInt(et_y.getText().toString());
        int r = Integer.parseInt(et_r.getText().toString());
        int r_in = Integer.parseInt(et_r_in.getText().toString());
        int color = Integer.parseInt(et_color.getText().toString());
        int alpha = sb_bar.getProgress();
        int a = Integer.parseInt(et_a.getText().toString());
        int b = Integer.parseInt(et_b.getText().toString());
        int c = Integer.parseInt(et_c.getText().toString());
        int time = Integer.parseInt(et_time.getText().toString());

        //保存数据到公共数据中
        PublicData.setData(x, y, r, r_in, color, alpha,a,b,c,time);

    }


    //判断输入框是否有空方法
    //有空返回true
    //没有空返回false
    private boolean haveEmpty(){
        if (et_x.getText().toString().isEmpty()){
            return true;
        }

        if (et_y.getText().toString().isEmpty()){
            return true;
        }

        if (et_r.getText().toString().isEmpty()){
            return true;
        }

        if (et_r_in.getText().toString().isEmpty()){
            return true;
        }

        if (et_color.getText().toString().isEmpty()){
            return true;
        }

        if (et_a.getText().toString().isEmpty()){
            return true;
        }

        if (et_b.getText().toString().isEmpty()){
            return true;
        }

        if (et_c.getText().toString().isEmpty()){
            return true;
        }

        if (et_time.getText().toString().isEmpty()){
            return true;
        }

        return false;
    }

    //从SharedPreferences中加载数据方法
    private void loadDataFromSP(){

        SharedPreferences sharedPreferences=getSharedPreferences("data",MODE_PRIVATE);

        int x = sharedPreferences.getInt("x", 0);
        int y = sharedPreferences.getInt("y", 0);
        int r = sharedPreferences.getInt("r", 0);
        int r_in = sharedPreferences.getInt("rIn", 0);
        int color = sharedPreferences.getInt("color", 0);
        int alpha = sharedPreferences.getInt("alpha", 0);

        boolean move = sharedPreferences.getBoolean("move", false);

        int a = sharedPreferences.getInt("a", 0);
        int b = sharedPreferences.getInt("b", 0);
        int c = sharedPreferences.getInt("c", 0);

        int time = sharedPreferences.getInt("time", 0);

        PublicData.setData(x, y, r, r_in, color, alpha,a,b,c,time);
        //还要将信息设置到组件上
        et_x.setText(String.valueOf(PublicData.x));
        et_y.setText(String.valueOf(PublicData.y));
        et_r.setText(String.valueOf(PublicData.r));
        et_r_in.setText(String.valueOf(PublicData.rIn));
        et_color.setText(String.valueOf(PublicData.color));
        sb_bar.setProgress(PublicData.alpha);


        et_a.setText(String.valueOf(PublicData.a));
        et_b.setText(String.valueOf(PublicData.b));
        et_c.setText(String.valueOf(PublicData.c));

        et_time.setText(String.valueOf(PublicData.time));

    }

    //销毁时方法
    @Override
    protected void onDestroy() {
        super.onDestroy();
        //移除悬浮窗
        if(FloatingWindowService.floatingView != null){
            if (FloatingWindowService.windowManager != null){
                //下一步判断
                if (FloatingWindowService.backgroundView!=null){
                    FloatingWindowService.windowManager
                            .removeView(FloatingWindowService.backgroundView);
                }

                FloatingWindowService.windowManager
                        .removeView(FloatingWindowService.floatingView);

                FloatingWindowService.windowManager = null;
            }
        }

        //监听器结束
        StorageService.over=true;
    }
}